//
//  ExamineOneViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/26.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "ExamineOneViewController.h"
#import "ExamineTabBar.h"
#import "ASValueTrackingSlider.h"
#import "GlobalFunction.h"
#import "WZYPickerView.h"
#import "NSString_extesion.h"
#import "ExamineOneDetailViewController.h"
#import "UserManger.h"
#import "ExamineLoginViewController.h"

@interface ExamineOneViewController ()<ASValueTrackingSliderDataSource>{
    ExamineTabBar                    *tabBarView;
}

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *oneLabel;
@property (weak, nonatomic) IBOutlet UILabel *twoLabel;
@property (weak, nonatomic) IBOutlet UILabel *threeLabel;
@property (weak, nonatomic) IBOutlet UILabel *fourLabel;
@property (weak, nonatomic) IBOutlet UIImageView *oneIcon;
@property (weak, nonatomic) IBOutlet UIImageView *twoIcon;
@property (weak, nonatomic) IBOutlet UIImageView *threeIcon;
@property (weak, nonatomic) IBOutlet UIImageView *fourIcon;
@property (weak, nonatomic) IBOutlet UILabel *lineLabel;
@property (weak, nonatomic) IBOutlet ASValueTrackingSlider *slider;
@property (weak, nonatomic) IBOutlet UIButton *qixianBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (nonatomic, strong) NSString * selectedString;

@end

@implementation ExamineOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"借款";
    tabBarView = [ExamineTabBar sharedExamineTabBar];
    [self.navigationItem setHidesBackButton:YES];
    
    float width = SCREEN_SCALE_320 * 308;
    float height = SCREEN_SCALE_320 * 175;
    float orginX = (SCREEN_WIDTH_DEVICE - width)/2;
    [self.bgImageView setFrame:CGRectMake(orginX, 8, width, height)];
    
    [self.contentView setFrame:CGRectMake(0, height + 16, SCREEN_WIDTH_DEVICE, 323)];
    
    [self.oneIcon setCenter:CGPointMake(self.oneLabel.center.x, self.oneIcon.center.y)];
    [self.twoIcon setCenter:CGPointMake(self.twoLabel.center.x, self.twoIcon.center.y)];
    [self.threeIcon setCenter:CGPointMake(self.threeLabel.center.x, self.threeIcon.center.y)];
    [self.fourIcon setCenter:CGPointMake(self.fourLabel.center.x, self.fourIcon.center.y)];
    [self.lineLabel setFrame:CGRectMake(self.oneIcon.frame.origin.x, 13, (SCREEN_WIDTH_DEVICE - self.oneIcon.frame.origin.x * 2 - 4), 1)];
    
    self.scrollView.alwaysBounceVertical = YES;
    self.scrollView.contentSize = CGSizeMake(0, 323 + height + 16);
    
    self.slider.minimumValue = 1000;
    self.slider.maximumValue = 10000;
    self.slider.value = 3000;
    self.slider.popUpViewCornerRadius = 5.0;
    [self.slider setMaxFractionDigitsDisplayed:0];
    self.slider.popUpViewColor = ThemeColor;
    self.slider.font = [UIFont fontWithName:@"GillSans-Bold" size:15];
    self.slider.textColor = [UIColor whiteColor];
    self.slider.popUpViewWidthPaddingFactor = 1.1;
    self.slider.dataSource = self;
    [self.slider showPopUpViewAnimated:YES];
    
    self.qixianBtn.layer.masksToBounds = YES;
    self.qixianBtn.layer.borderWidth = 1.0f;
    self.qixianBtn.layer.borderColor = LineColor.CGColor;
    
    [self.sureBtn setBackgroundImage:getColorToImage(ThemeColor) forState:UIControlStateNormal];
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = 5.0f;
    
    _selectedString = @"1个月";
    
    [self.scrollView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - 64 - IS_iPhoneX_Top - 50 - IS_iPhoneX_Bottom)];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:1];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)qixianPress {
    NSMutableArray *arr = [NSMutableArray array];
    NSArray *qixianArr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",nil];
    for (NSString *contentStr in qixianArr) {
        [arr addObject:[NSString qixianChange:[contentStr integerValue]]];
    }
    
    if (_selectedString.length == 0) {
        _selectedString = [arr objectAtIndex:0];
    }
    [WZYPickerView showPickerViewInView:self.view
                            withStrings:arr
                            withOptions:@{MMbackgroundColor: [UIColor whiteColor],
                                          MMtextColor: TextColor,
                                          MMtoolbarColor: BackgroundColor,
                                          MMbuttonColor: ThemeColor,
                                          MMfont: [UIFont systemFontOfSize:17],
                                          MMvalueY: @5,
                                          MMselectedObject:_selectedString,
                                          MMtextAlignment:@1}
                             completion:^(NSString *chooseString, NSInteger row, BOOL doneBool) {
                                 if (doneBool) {
                                     [self.qixianBtn setTitle:chooseString forState:UIControlStateNormal];
                                     _selectedString = chooseString;
                                 }
                                 
                             }];
}

- (IBAction)surePress {
    if ([UserManger hasUserLogged]) {
        ExamineOneDetailViewController *viewCon = [[ExamineOneDetailViewController alloc] initWithNibName:@"ExamineOneDetailViewController" bundle:nil];
        viewCon.amount = self.slider.value;
        viewCon.qixian = _selectedString;
        [self.navigationController pushViewController:viewCon animated:YES];
    }else{
        ExamineLoginViewController *viewCon = [[ExamineLoginViewController alloc] initWithNibName:@"ExamineLoginViewController" bundle:nil];
        viewCon.RefreshHandle = ^(){
            [self surePress];
        };
        CustomNavigationController *navCon = [[CustomNavigationController alloc] initWithRootViewController:viewCon];
        [navCon setNavigationBarStyle:NO];
        [self.navigationController presentViewController:navCon animated:YES completion:^{
        }];
    }
    
}

- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value;
{
    value = roundf(value);
    NSInteger nearestNumber = lround(value);
    NSInteger num = nearestNumber / 100;
    
    NSString *s = [NSString stringWithFormat:@"%ld", num * 100];
    return s;
}

@end
